﻿////////////////////////////////////////////////////////////////////////////////
//
//  Copyright (C) 2003-2006 Adobe Macromedia Software LLC and its licensors.
//  All Rights Reserved. The following is Source Code and is subject to all
//  restrictions on such code as contained in the End User License Agreement
//  accompanying this product.
//
////////////////////////////////////////////////////////////////////////////////

package ru.vega_int.firsoff.as3.utils
{

/**
 *  The ArrayUtil utility class is an all-static class
 *  with methods for working with arrays within Flex.
 *  You do not create instances of ArrayUtil;
 *  instead you call static methods such as the 
 *  <code>ArrayUtil.toArray()</code> method.
 */
public class ArrayUtil
{
	
	//--------------------------------------------------------------------------
	//
	//  Class methods
	//
	//--------------------------------------------------------------------------

    /**
     *  Ensures that an Object can be used as an Array.
	 *
     *  <p>If the Object is already an Array, it returns the object. 
     *  If the object is not an Array, it returns an Array
	 *  in which the only element is the Object.
	 *  As a special case, if the Object is null,
	 *  it returns an empty Array.</p>
	 *
     *  @param obj Object that you want to ensure is an array.
	 *
     *  @return An Array. If the original Object is already an Array, 
     * 	the original Array is returned. Otherwise, a new Array whose
     *  only element is the Object is returned or an empty Array if 
     *  the Object was null. 
     */
    public static function toArray(obj:Object):Array
    {
		if (!obj) 
			return [];
		
		else if (obj is Array)
			return obj as Array;
		
		else
		 	return [ obj ];
    }
    
    /**
     *  Returns the index of the item in the Array.
     * 
     *  Note that in this implementation the search is linear and is therefore 
     *  O(n).
     * 
     *  @param item The item to find in the Array. 
     *
     *  @param source The Array to search for the item.
     * 
     *  @return The index of the item, and -1 if the item is not in the list.
     */
    public static function getItemIndex(item:Object, source:Array):int
    {
        var n:int = source.length;
        for (var i:int = 0; i < n; i++)
        {
            if (source[i] === item)
                return i;
        }

        return -1;           
    };
	
	/**
	 *    
	 * @param	arr1
	 * @param	arr2
	 * @param	field	 
	 * @return	  {equally:Array, different:Array, rezult:int}
	 */
	public static function compareArrays(arr1:Array, arr2:Array, field:*=null):Object
	{
		var out:Object = new Object();
		out.rezult = -1;
		var str1:String = arr1.join('*');
		var str2:String = arr2.join('*');
		var summ:String = str1.concat(str2);
		
		var maxArr:Array = arr1.concat(arr2); //(arr1.length > arr2.length) ? arr1 : arr2;
		var n:int = maxArr.length;
		//	    
		var tempEqually:Array = new Array();
		var tempDifferent:Array = new Array();
		
		for (var i:int = 0; i < n; i++) 
		{
			
			// 
			var s1:int = str1.indexOf(maxArr[i]);
			var s2:int = str2.indexOf(maxArr[i]);
			if (s1 > -1 && s2 >-1)
			{
				if (maxArr[i] != maxArr[i-1])
				{
					tempEqually.push(maxArr[i]);
				}
			}
			else 
			{
				if (maxArr[i] != maxArr[i-1])
				{
					tempDifferent.push(maxArr[i]);
				};
			};
			
			/*trace('массив = ' + maxArr[i]) ;
			trace('строка1 = ' + str1);
			trace('строка2 = ' + str2);
			trace('результат 1 = ' + s1);
			trace('результат 2 = ' + s2);
			trace('rezult = ' +  (s1 > -1 && s2 >-1));
			trace('------------------------------');*/
			
		};
		
		//  
		out.equally = removeDuplicates(tempEqually);
		out.different = removeDuplicates(tempDifferent);
		return out;
	};
	
	/**
	 * Удаление дубликатов
	 * @param	arr
	 * @return
	 */
	public static function removeDuplicates(arr:Array) : Array
	{
		var out:Array = new Array(arr);
		var n:int = arr.length;
		var k:int = arr.length;
		var member:*;
		for (var i:int = 0; i < n; i++) 
		{
			member = arr[i];
			for (var j:int = i+1; j < k; j++) 
			{
				//trace(member, arr[j]);
				if (member == arr[j])
				{
					//trace('delete = ' + arr[j] + '  from: ' + j)
					arr.splice(j, 1);
				};
			};
			//trace('arr = '+arr);
			//trace('------------------')
		};
		return arr;
	};
	
	/**
	 * Нарастающий итог 
	 * @param	arr Входной массив
	 * @return	Массив нарастающим итогом
	 */
	public static function accruingResult(arr:Array) : Array
	{
		var i:int = 0;
		var n:int = arr.length;
		var s:int = 0;
		var retArr:Array = new Array();
		for (i; i < n; i++) 
		{
			var j:int = 0;
			s = 0;
			for (j; j <= i; j++) 
			{
				s += Number(arr[j]);
			};
			retArr.push(s);
		};
		return retArr;
	};
};

}
